package com.mit.service.manufacture.dao.impl;

import java.util.HashMap;
import java.util.List;

import com.mit.common.exception.NoRecordUpdateException;
import com.mit.common.manufacture.dto.FactoryDTO;
import com.mit.service.core.dao.AbstractDAO;
import com.mit.service.manufacture.dao.IFactoryDAO;


public class FactoryDAOImpl extends AbstractDAO implements IFactoryDAO {

	@Override
	public FactoryDTO createFactoryDTO(FactoryDTO to) {
		Integer id = (Integer) getSqlMapClientTemplate().insert("insertFactory", to);
		to.setId(id);
		return to;
	}

	@Override
	public List<FactoryDTO> retrievePagedFactorys(Integer orgId, String searchKey, Integer pageNo, Integer pageSize) {
		FactoryDTO to = new FactoryDTO();
		to.setName(searchKey);
		to.setPageNo(pageNo);
		to.setPageSize(pageSize);
		to.setOrgId(orgId);
		return getSqlMapClientTemplate().queryForList("selectPagedFactorys", to);
	}

	@Override
	public List<FactoryDTO> retrieveFactoryBasicInfo(Integer orgId, String searchKey) {
		HashMap param = new HashMap();
		param.put("name", searchKey);
		param.put("orgId", orgId);
		return getSqlMapClientTemplate().queryForList("selectFactoryBasicInfo", param);
	}

	@Override
	public Integer retrieveFactorysCount(Integer orgId, String searchKey) {
		FactoryDTO to = new FactoryDTO();
		to.setName(searchKey);	
		to.setOrgId(orgId);
		return (Integer) getSqlMapClientTemplate().queryForObject("selectFactorysCount", to);
	}

	@Override
	public FactoryDTO updateFactoryDTO(FactoryDTO to)
			throws NoRecordUpdateException {
		int rows = getSqlMapClientTemplate().update("updateFactory",to);
		if(rows < 1)
			throw new NoRecordUpdateException();
		return to;
	}

	@Override
	public boolean hasFactory(Integer orgId, Integer id, String name) {
		HashMap param = new HashMap();
		param.put("id", id);
		param.put("name", name);
		param.put("orgId", orgId);
		Integer rowCount = (Integer) getSqlMapClientTemplate().queryForObject("selectFactoryByName", param);
		return (rowCount > 0);
	}

}
